package com.bub.pay.core.util.runnable;

import com.bub.pay.framework.common.util.SpringUtil;

/**
 * Runnable 的Spring包装类 让Runnable可以在Spring周期里运行
 *
 * @author recall
 * @date 2019/8/3
 */
public class SpringRunnableWrap<E extends Runnable> implements Runnable {

    private volatile E runnable;
    private Class<E> eClass;
    private Object[] args;

    public SpringRunnableWrap(Class<E> eClass, Object... args) {
        this.eClass = eClass;
        this.args = args;
    }

    /**
     * 获取Runnable
     *
     * @return Runnable
     */
    private E getRunnable() {
        if (runnable == null) {
            synchronized (this) {
                if (runnable == null) {
                    runnable = SpringUtil.getBean(eClass, args);
                }
            }
        }
        return runnable;
    }


    @Override
    public void run() {
        getRunnable().run();
    }

}
